/****************************************************
 * JS对象工具类
 ****************************************************/

/**
 * 自定义Map对象
 * put 放入一个Key-Value
 * get 获取一个Value
 * remove 移除一个值
 * each(fn) 针对每一个对象执行函数
 * entrys 转换为数组对
 * isEmpty 是否为空
 * size 长度 
 * toString 转换为String
 */
function Map() {
	/** 存放键的数组(遍历用到) */
	this.keys = new Array();
	/** 存放数据 */
	this.data = new Object();
	
	/**
	 * 放入一个键值对
	 * @param {String} key
	 * @param {Object} value
	 */
	this.put = function(key, value) {
		if(this.data[key] == null){
			this.keys.push(key);
		}
		this.data[key] = value;
	};
	
	/**
	 * 获取某键对应的值
	 * @param {String} key
	 * @return {Object} value
	 */
	this.get = function(key) {
		return this.data[key];
	};
	
	/**
	 * 删除一个键值对
	 * @param {String} key
	 */
	this.remove = function(key) {
		this.keys.baoremove(key);
		this.data[key] = null;
	};
	
	/**
	 * 遍历Map,执行处理函数
	 * 
	 * @param {Function} 回调函数 function(key,value,index){..}
	 */
	this.each = function(fn){
		if(typeof fn != 'function'){
			return;
		}
		var len = this.keys.length;
		for(var i=0;i<len;i++){
			var k = this.keys[i];
			fn(k,this.data[k],i);
		}
	};
	
	/**
	 * 获取键值数组(类似Java的entrySet())
	 * @return 键值对象{key,value}的数组
	 */
	this.entrys = function() {
		var len = this.keys.length;
		var entrys = new Array(len);
		for (var i = 0; i < len; i++) {
			entrys[i] = {
				key : this.keys[i],
				value : this.data[i]
			};
		}
		return entrys;
	};
	
	/**
	 * 判断Map是否为空
	 */
	this.isEmpty = function() {
		return this.keys.length == 0;
	};
	
	/**
	 * 获取键值对数量
	 */
	this.size = function(){
		return this.keys.length;
	};
	
	/**
	 * 重写toString 
	 */
	this.toString = function(){
		var s = "{";
		for(var i=0;i<this.keys.length;i++,s+=','){
			var k = this.keys[i];
			s += k+"="+this.data[k];
		}
		s+="}";
		return s;
	};
	
	this.valueString = function(){
		//alert(this.keys)
		var s = "";
		for(var i=0;i<this.keys.length;i++){
			var k = this.keys[i];
			var v = this.data[k];
			if(v!=null&&v!="null")
			{
				s += v ;
				s+=',';
			}
		}
		if(s.substr(s.length-1,s.length)==",")
		{
			s = s.substring(0, s.length-1);
		}
		return s;
	};
	
	this.keyString = function(){
		//alert(this.keys)
		var s = "";
		for(var i=0;i<this.keys.length;i++){
			var k = this.keys[i];
			var v = this.data[k];
			if(v!=null&&v!="null")
			{
				s += k ;
				s+=',';
			}
		}
		if(s.substr(s.length-1,s.length)==",")
		{
			s = s.substring(0, s.length-1);
		}
		return s;
	};
	
} 

Array.prototype.baoremove = function(dx){
	for(var i=0;i<this.length;i++){ 
		if(this[i]==dx){
			dx = i;
			break;
		}
    }
	if(isNaN(dx)||dx>this.length){
		return false;
	}
	this.splice(dx,1);
};